import json
import os
import pickle

from python_part.main_policy_database_member.ProtectingEntities import ProtectingEntities
from python_part.main_policy_database_member.ReadWriteClass import ReadWriteClass

from GlobaLogger import Log
log = Log(__name__).getlog()

class PolicyStoreModule:
    def __init__(self, policy_save_path, tmp_path):
        self.policy_save_path: str = policy_save_path
        self.tmp_path: str = tmp_path
        self.entity_path_to_rwclass_id: dict[str, int] = {}
        self.rwclass_id_to_rwclass: dict[int, ReadWriteClass] = {}
        self.global_config_dict: dict = {}

    def init_database(self,
                      entity_path_to_rwclass_id: dict[str, int],
                      rwclass_id_to_rwclass: dict[int, ReadWriteClass],
                      global_config_dict: dict,
                      ):
        self.entity_path_to_rwclass_id = entity_path_to_rwclass_id
        self.rwclass_id_to_rwclass = rwclass_id_to_rwclass
        self.global_config_dict = global_config_dict

        self.__save_one_dict(self.entity_path_to_rwclass_id, "entity_path_to_rwclass_id")
        self.__save_one_dict(self.rwclass_id_to_rwclass, "rwclass_id_to_rwclass")
        self.__save_one_dict(self.global_config_dict, "global_config_dict")

    def load_database_from_file(self, save_path):
        tmp = self.policy_save_path
        try:
            self.policy_save_path = save_path
            self.entity_path_to_rwclass_id = self.__load_one_dict("entity_path_to_rwclass_id")
            self.rwclass_id_to_rwclass = self.__load_one_dict("rwclass_id_to_rwclass")
            self.global_config_dict = self.__load_one_dict("global_config_dict")
        except:
            self.policy_save_path = tmp
        self.policy_save_path = tmp

    def __save_one_dict(self, dict_need_save, save_name):
        save_path = os.path.join(self.policy_save_path, save_name)
        # with open(save_path + '.json', 'w+') as file:
        #     json.dump(dict_need_save, file)

        with open(save_path + '.pickle', 'wb+') as file:
            pickle.dump(dict_need_save, file)

    def __load_one_dict(self, save_name) -> dict:
        data = -1
        save_path = os.path.join(self.policy_save_path, save_name)
        with open(save_path + '.pickle', 'rb') as file:
            data = pickle.load(file)
        return data

    def show_all(self):
        print("entity_path_to_rwclass_id:")
        for i in self.entity_path_to_rwclass_id:
            print(f"{i} -> {self.entity_path_to_rwclass_id[i]}")
        print("rwclass_id_to_rwclass:")
        for i in self.rwclass_id_to_rwclass:
            print(f"{i} -> {self.rwclass_id_to_rwclass[i]}")
        print("global_config_dict:")
        for i in self.global_config_dict:
            print(f"{i} : {self.global_config_dict[i]}")

    def get_all_policy(self) -> tuple[dict, dict, dict]:
        return (self.entity_path_to_rwclass_id,
                self.rwclass_id_to_rwclass,
                self.global_config_dict)

    def update_entity_path_to_rwclass_id(self, which_key:str, value_need_in:int) -> None:
        self.entity_path_to_rwclass_id[which_key] = value_need_in
        self.save_change_to_file()

    def update_rwclass_id_to_rwclass(self, which_key:int, value_need_in:ReadWriteClass):
        self.rwclass_id_to_rwclass[which_key] = value_need_in
        self.save_change_to_file()

    def delete_entity_path_to_rwclass_id(self, which_key:str):
        if which_key in self.entity_path_to_rwclass_id:
            del self.entity_path_to_rwclass_id[which_key]
        self.save_change_to_file()

    def delete_rwclass_id_to_rwclass(self,  which_key:int):
        if which_key in self.rwclass_id_to_rwclass:
            del self.rwclass_id_to_rwclass[which_key]
        self.save_change_to_file()

    def save_change_to_file(self):
        pass
